{% extends "sys.html" %}

{% block tabs %}{{show_tabs('users')}}{% endblock %}

{% block clientscript %}
	{{super()}}
	<script>
		function edit_user(uid) {
		    $('#dlg').dialog({
						title: '用户管理',
						width: 370,
						height: 320,
						closed: false,
						cache: false,
						modal: true
					});
			 var url='/edit_user?uid='+(uid?uid:'');
			 $('#dlg').dialog('refresh',url);
		}
		
		function update_user_status(uid,stat,op_name) {
		     var msg="确定要["+op_name+"]该用户吗？"
		     		url="/update_user_status/"+uid+"/"+stat;
		         $.messager.confirm("确认", msg, function (r) {
		                if (r) {
		                    $.post(url,function(data,status){
		                    		if (data=='1') {
		                    			$('#gri').datagrid('reload');
		                    		} else {
		                    			alert(data);
		                    		}
		                    });
		                }
		            });
		}
		
		function fmt_op(val,row) {
			{% if g.user.is_admin %}
				if (row.is_admin=='T'){
					return '';
				}
				var stat,op_name;
				if (row.status=='STA_ACTIVE') {
					stat=0;
					op_name='禁用';
				} else {
					stat=1;
					op_name='启用';
				}
				return "[<a href='javascript:void(0)' onclick='edit_user("+row.id+")'>编辑</a>]&nbsp;&nbsp;"
						+"[<a href='javascript:void(0)' onclick='update_user_status("+row.id+","+stat+",\""+op_name+"\")'>"+op_name+"</a>]";

			{% endif %}
		}
	</script>
{% endblock %}

{% block work_content %}
<div id="tb" style="padding:3px">
        <a href="javascript:void(0)" class="easyui-linkbutton" data-options="plain:true,iconCls:'icon-add'" onclick="edit_user()">新增用户</a>
     </div>
    {% from "snippet.html" import render_field %}
    <div id="dlg" closed="true" class="easyui-dialog" data-options="buttons: [{
            text:'确定',
            iconCls:'icon-ok',
            handler:function(){
                $.ajax({
                    type: 'post',
                    url: $('#form_post').attr('action'),
                    data:$('#form_post').serialize(),
                    dataType: 'text',
                    success: function(data,textStatus){
                        if (data=='1'){
                            $('#gri').datagrid('reload');
                            $('#dlg').dialog('close');
                        } else {
                            $.messager.alert('错误',data,'error');
                        }

                    },
                    error: function(err){
                        //请求出错处理
                        $.messager.alert('错误','操作失败,请稍后重试!','error');
                    }
                });
            }
        },{

            text:'取消',
            iconCls:'icon-cancel',
            handler:function(){
                $('#dlg').dialog('close');
            }
        }]">
    </div>

    <div class='grid'>
	    <table id="gri" class="easyui-datagrid" style="height:600px"
	            url="{{ url_for('qry_user_data') }}"
	            title=""
	            toolbar="#tb"
	            singleSelect="true"
	            pageSize="20"
	            fitColumns="true"
	            rownumbers="true" pagination="true">
	        <thead>
	            <tr>
               	<th field="username" width="50" >用户名称</th>
                  <th field="remarkname" width="50" >备注名</th>
	               <th field="status_name" width="40">状态</th>
                  <th field="email" width="50" >邮箱</th>
	               <th field="is_admin" width="50" >是否是管理员</th>
	               <th field="created_date" width="60" >创建日期</th>
					   <th field="op" width="50" formatter="fmt_op" >操作</th>
	            </tr>
	        </thead>
	    </table>
    </div>
{% endblock %}